﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

using FineUI;
using ZHDJ.Core.Caching;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Org;
using ZHDJ.WebPage;
using ZHDJ.WebPage.Picker;

namespace ZHDJ.Web.App.Share.Picker
{
    public partial class MemberSinglePicker : PickerPageBase
    {
       /// <summary>
        /// 
        /// </summary>
        public string BranchID
        {
            get { return Request.QueryString["BranchID"]??""; }
        }
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                BindTree();
                LoadData();
            }
        }

        private void BindData()
        {
            //绑定已选择的值
            var selected = SelectValue;
            if (selected.Count < 1)
                return;
            var selectedUser = DB.PartyOrgMembers.Where(a => selected.Contains(a.LoginID));
            //if (selectedUser.Count() < 1)
            //    return;
            var d = from u in selectedUser select u.LoginID + "|" + u.UserName;
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            this.hdParentBranchID.Text = BranchID;
        }

        private void LoadData()
        {
            BindGrid();
        }


        private void BindTree()
        {

            TreeControlBusiness tree = new TreeControlBusiness("BranchID", "ParentBranchID", "BranchName", true, CacheKeys.Cache_Branch_Tree);
            tree.EnableClickEvent = true;
            tree.BindTreeBranch(TreeBranch);
            if (!string.IsNullOrEmpty(BranchID))
            {
                var n = TreeBranch.FindNode(BranchID);
                TreeBranch.Nodes.Clear();
                if (n != null)
                    TreeBranch.Nodes.Add(n);
            }
        }

    
      
        /// <summary>
        /// 绑定用户表
        /// </summary>
        private void BindGrid()
        {
            var ds = PartyOrgMember.GetMemberByBranchId(hdParentBranchID.Text,
                ttbSearchMessage.Text.Trim(), Grid1.PageIndex, Grid1.PageSize, 0);
            Grid1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            Grid1.DataSource = ds.Tables[0];
            Grid1.DataBind();
           
        }

        #endregion

        #region Events
        protected void TreeBranch_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            hdParentBranchID.Text = e.Node.NodeID;
            BindGrid();
        }
        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
            ttbSearchMessage.ShowTrigger1 = true;
            BindGrid();
        }

        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            ttbSearchMessage.Text = String.Empty;
            ttbSearchMessage.ShowTrigger1 = false;
            BindGrid();
        }


       

        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }


        
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {

            string userID = ControlBusiness.GetSelectedDataKeyID(Grid1);
            string userName = ControlBusiness.GetSelectedDataKey(Grid1, 1);
            ReturnValue(userID, userName);
        }
        #endregion

    }
}